package com.example.base.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Getter
public enum DateFormatEnum {

    NYR("yyyy-MM-dd", DateTimeFormatter.ofPattern("yyyy-MM-dd")),

    NYR_SFM("yyyy-MM-dd HH:mm:ss", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    private String format;

    private DateTimeFormatter formatter;

    DateFormatEnum(String format, DateTimeFormatter formatter){
        this.format = format;
        this.formatter = formatter;
    }

    public static LocalDate strToLocalDate(DateFormatEnum formatEnum, String date){
        if (StringUtils.isBlank(date)){
            return null;
        }
        try {
            return LocalDate.parse(date, formatEnum.formatter);
        }catch (Exception e){
            return null;
        }
    }

    public static LocalDateTime strToLocalDateTime(DateFormatEnum formatEnum, String date){
        if (StringUtils.isBlank(date)){
            return null;
        }
        try {
            return LocalDateTime.parse(date, formatEnum.formatter);
        }catch (Exception e){
            return null;
        }
    }
}
